package net.lab1024.sa.admin.module.business.erp.sao.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.inv.dao.InventordDao;
import net.lab1024.sa.admin.module.business.erp.inv.domain.entity.InventordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MmordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MmordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrqordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrqordbEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MrqordaAddForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MrqordbAddForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MrqordaVO;
import net.lab1024.sa.admin.module.business.erp.poo.dao.PuordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.PuordaEntity;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SaordaDao;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SiordDao;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SiordaDao;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SiordbDao;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SaordaEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SiordEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SiordaEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SiordbEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.form.*;
import net.lab1024.sa.admin.module.business.erp.sao.domain.vo.SaordaVO;
import net.lab1024.sa.admin.module.business.erp.sao.domain.vo.SiordVO;
import net.lab1024.sa.admin.module.business.erp.sao.domain.vo.SiordaVO;
import net.lab1024.sa.admin.module.business.erp.sao.domain.vo.SnordVO;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 销售发货单 Service
 *
 * @Author AKITA
 * @Date 2025-08-27 14:33:47
 * @Copyright AKITA
 */

@Service
public class SiordService {

    @Resource
    private SiordDao siordDao;
    @Resource
    private SiordaDao siordaDao;
    @Resource
    private InventordDao inventordDao;
    @Resource
    private SiordbDao siordbDao;
    @Resource
    private SaordaDao saordaDao;



    public PageResult<SiordVO> queryPage(SiordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<SiordVO> list = siordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }


    public ResponseDTO<Long> add(SiordAddForm addForm) {


        /*
         * 1、校验当前批次物料是否
         *
         *
         *
         * */

        SiordEntity siordEntity = SmartBeanUtil.copy(addForm, SiordEntity.class);
        siordDao.insert(siordEntity);


        int lineNum = 1;

        for (SiordaAddForm mrqordaAddForm : addForm.getLista()) {
            SiordaEntity copy = SmartBeanUtil.copy(mrqordaAddForm, SiordaEntity.class);
            copy.setId(siordEntity.getId());
            siordaDao.insert(copy);

            for (SiordbAddForm mrqordbAddForm : mrqordaAddForm.getLista()) {
                SiordbEntity copy1 = SmartBeanUtil.copy(mrqordbAddForm, SiordbEntity.class);
                copy1.setId(siordEntity.getId());
                copy1.setLineNum(lineNum++);
                copy1.setBaseId(siordEntity.getId());
                copy1.setBaseLineNum(copy.getLineNum());
                siordbDao.insert(copy1);
                InventordEntity inventordEntity = inventordDao.selectOne(new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbAddForm.getItemId()).eq(InventordEntity::getBatchId, mrqordbAddForm.getBatchId()));
                if (inventordEntity.getQty().compareTo(mrqordbAddForm.getQty()) >= 0) {
                    inventordEntity.setQty(inventordEntity.getQty().subtract(mrqordbAddForm.getQty()));
                    inventordDao.update(inventordEntity, new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbAddForm.getItemId()).eq(InventordEntity::getBatchId, mrqordbAddForm.getBatchId()));
                }


                SaordaEntity saordaVO = saordaDao.selectOneBySi(copy.getBaseId(),copy.getBaseLineNum());
                saordaVO.setOutQty(saordaVO.getOutQty().add(copy1.getQty()));
                saordaDao.update(saordaVO,new LambdaQueryWrapper<SaordaEntity>().eq(SaordaEntity::getId,saordaVO.getId()).eq(SaordaEntity::getLineNum,saordaVO.getLineNum()));

            }

        }


        return ResponseDTO.ok(siordEntity.getId());
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(SiordUpdateForm updateForm) {
        SiordEntity siordEntity = SmartBeanUtil.copy(updateForm, SiordEntity.class);
        siordDao.updateById(siordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        siordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }


    @Transactional
    public ResponseDTO<String> delete(Long id) {




        List<SiordbEntity> mrqordbEntities = siordbDao.selectList(new LambdaQueryWrapper<SiordbEntity>().eq(SiordbEntity::getId, id));

        for (SiordbEntity mrqordbEntity : mrqordbEntities) {

            InventordEntity inventordEntity = inventordDao.selectOne(new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbEntity.getItemId()).eq(InventordEntity::getBatchId, mrqordbEntity.getBatchId()));

            inventordEntity.setQty(inventordEntity.getQty().add(mrqordbEntity.getQty()));

            inventordDao.update(inventordEntity, new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getItemId, mrqordbEntity.getItemId()).eq(InventordEntity::getBatchId, mrqordbEntity.getBatchId()));

            SiordaEntity siordaEntity = siordaDao.selectOne(new LambdaQueryWrapper<SiordaEntity>().eq(SiordaEntity::getId, mrqordbEntity.getId()).eq(SiordaEntity::getLineNum, mrqordbEntity.getLineNum()));



//            SaordaEntity saordaVO = saordaDao.selectOneBySi(siordaEntity.getBaseId(),siordaEntity.getBaseLineNum());

//            saordaVO.setOutQty(saordaVO.getOutQty().subtract(mrqordbEntity.getQty()));

//            saordaDao.update(saordaVO,new LambdaQueryWrapper<SaordaEntity>().eq(SaordaEntity::getId,saordaVO.getId()).eq(SaordaEntity::getLineNum,saordaVO.getLineNum()));
        }


        siordDao.deleteById(id);
        siordaDao.deleteById(id);
        siordbDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<SiordVO> info(Long id) {
        SiordVO copy = SmartBeanUtil.copy(siordDao.selectById(id), SiordVO.class);
        List<SiordaVO> mrqordaVOS = siordaDao.selectLista(id);
        copy.setLista(mrqordaVOS);
        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<SiordVO> infoUnexecute(Long id) {
        List<SiordVO> poordVOS = siordDao.infoUnexecute(id);
        return ResponseDTO.ok(poordVOS.get(0));





    }
}
